package com.ctrip.ec.api.mobile.controller;


import com.ctrip.ec.api.mobile.common.ValidateException;
import com.ctrip.ec.api.mobile.model.ImageModel;
import com.ctrip.ec.biz.dao.image.ImageDao;
import com.ctrip.ec.biz.dao.sequence.SequenceDao;
import com.ctrip.ec.biz.entity.image.ImageEntity;
import com.ctrip.ec.biz.image.ImageUpload;
import com.ctrip.ec.dal.PagerSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("image")
public class ImageController {
    @Autowired
    private ImageDao image;

    @Autowired
    @Qualifier("imageUpload")
    private  ImageUpload upload;

    @Value("${imageDomain}")
    private  String imageDomain;

    @Autowired
    private SequenceDao sequence;

    @GetMapping("list")
    public List<ImageEntity> list() throws SQLException {
        List<ImageEntity> list = image.list();
        return list;
    }



    @GetMapping("detail/{id}")
    public ImageEntity detail(@PathVariable Long id) throws SQLException {
        return image.entity(id);
    }

    @PostMapping("save")
    public Long save(@RequestBody ImageEntity entity) throws SQLException {
        image.save(entity);
        return entity.getId();
    }

    @PostMapping("delete")
    public void delete(@RequestBody List<Long> ids) throws SQLException {
        image.delete(ids);
    }

    @PostMapping("erase")
    public void erase(@RequestBody List<Long> ids) throws SQLException {
        image.erase(ids);
    }

    @PostMapping("unerase")
    public void unerase(@RequestBody List<Long> ids) throws SQLException {
        image.unerase(ids);
    }

    @PostMapping("upload")
    public ImageModel upload(@RequestParam("file") MultipartFile file,
                             HttpServletRequest request) throws SQLException, IOException {
        // String contentType = file.getContentType();
        String originalFilename = file.getOriginalFilename();

        String type = originalFilename.indexOf(".") != -1 ?
                originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length()) : null;

        type= type.toLowerCase();

        if (!(type.equals("jpg") || type.equals("jpeg") || type.equals("png"))) {
            throw new ValidateException("图片格式不对");
        }

        BufferedImage img = ImageIO.read(file.getInputStream());


        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        uuid += "_" + img.getWidth() + "_" + img.getHeight();

        String fileName = uuid + "." + type;

        String url = upload.upload(fileName,file.getInputStream());

        ImageEntity imageEntity = new ImageEntity();
        imageEntity.setName(originalFilename);
        imageEntity.setCreateTime(new Timestamp(System.currentTimeMillis()));
        imageEntity.setInUse(false);
        imageEntity.setUrl(url);
        imageEntity.setWidth(img.getWidth());
        imageEntity.setHeight(img.getHeight());
        imageEntity.setSize(file.getSize());
        image.save(imageEntity);

        return new ImageModel(imageEntity.getId(), imageEntity.getUrl(), imageDomain + url);

    }
    //返回json
    //return "uploadimg success";
}
